package com.gaoxk.distributeReq;

import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import java.util.UUID;

//import com.alibaba.druid.support.http.StatViewServlet

/**
 * 增加过滤
 * @className: ChannelFilter
 * @copyright: Copyright (C) 2019-2028, Inc. All rights reserved.
 * @company: 中兴新云.财务云
 * @author: 罗明亮
 * @date: 2020/7/22 20:17
 */
//@Component
@WebFilter(urlPatterns = "/BONDService/*",filterName = "securityRequestFilter")
public class ChannelFilter implements Filter {
    private final Logger logger = LoggerFactory.getLogger(ChannelFilter.class.getName());
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //com.alibaba.druid.
        ServletRequest requestWrapper = null;
        ResponseWrapper wrapperResponse = null;
        //将该请求的URL从协议名称返回到HTTP请求的第一行中的查询字符串。
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String requestURI = httpServletRequest.getRequestURI();
        String logid = UUID.randomUUID().toString().replace("-","");
        ThreadContext.put("logid", logid);
        logger.info("请求开始:[{}]",logid);
        logger.info("访问的url:"+requestURI);
        //logger.info("bbbbbb");
//        if(requestURI.startsWith("/druid")) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
            if(servletRequest instanceof HttpServletRequest) {
                logger.info("servletRequest instanceof HttpServletRequest");
                requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
                wrapperResponse = new ResponseWrapper((HttpServletResponse)servletResponse);
            }

            if(requestWrapper == null) {
                logger.info("requestWrapper为空");
                filterChain.doFilter(servletRequest, servletResponse);
            } else {
                //logger.info("222");
                logger.info("请求报文[{}]",((RequestWrapper)requestWrapper).getBody());
                //filterChain.doFilter(requestWrapper, servletResponse);
                filterChain.doFilter(requestWrapper, wrapperResponse);
                String content = wrapperResponse.getTextContent();
                logger.info("应答报文[{}]",content);
                servletResponse.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
            }
        logger.info("请求结束:[{}]",logid);
        //}
    }

    @Override
    public void destroy() {

    }
}
